package com.kfm.servlet.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/session")
public class SessionServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取 Session 对象
        HttpSession session = req.getSession();

        // 设置 Session 的最大存活时间, 单位为秒
        session.setMaxInactiveInterval(60 * 30);

        System.out.println(session);
        System.out.println("sessionID : " +session.getId());
        System.out.println("isNew : " + session.isNew());
        System.out.println("CreationTime : " + session.getCreationTime());
        System.out.println("LastAccessedTime : " + session.getLastAccessedTime());
        System.out.println("MaxInactiveInterval : " + session.getMaxInactiveInterval());

        // session 域中存放数据
        session.setAttribute("name", "kfm1");
    }
}
